package com.jujia.tmall.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jujia.tmall.base.BaseActivity;
import com.jujia.tmall.base.DoubleClickUtil;
import com.jujia.tmall.base.SealConst;
import com.jujia.tmall.cache.ServerAddress;
import com.jujia.tmall.cache.StringEluneCallback;
import com.jujia.tmall.ui.ClearEditText;
import com.jujia.tmall.ui.PassSeeEditTextNoEmoji;
import com.jujia.tmall.util.NToast;
import com.jujia.tmall.util.StatusBarDrawableUtil;
import com.jujia.tmall.util.ToolsKit;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;


/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.login_btn)
    TextView loginBtn;

    @Bind(R.id.edit_phone_number)
    ClearEditText mAccountEdit;
    @Bind(R.id.edit_pwd)
    PassSeeEditTextNoEmoji mPasswordEdit;

    //    private boolean isOtherLogin = false;
    private Context mContext;

    private String accountString;
    private String passwordString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mContext = this;
        StatusBarDrawableUtil.setColor(this, ContextCompat.getDrawable(this, R.color.colorTheme));
        initMyView();

    }


    private void initMyView() {

        setMenuTitle(1, 0, SealConst.APP_NAME, 0, 0);
        if (ToolsKit.isStringNotEmpty(spBase.getString(SealConst.SEALTALK_LOGING_ACCOUNT, ""))) {
            mAccountEdit.setText(spBase.getString(SealConst.SEALTALK_LOGING_ACCOUNT, ""));
        } else {
            mAccountEdit.setText("");
        }

        if (ToolsKit.isStringNotEmpty(spBase.getString(SealConst.SEALTALK_LOGING_PASSWORD, ""))) {
            mPasswordEdit.setText(spBase.getString(SealConst.SEALTALK_LOGING_PASSWORD, ""));
        } else {
            mPasswordEdit.setText("");
        }

        loginBtn.setOnClickListener(this);


    }


    private void checkLogin() {
        accountString = mAccountEdit.getText().toString().trim();
        passwordString = mPasswordEdit.getText().toString().trim();

        if (TextUtils.isEmpty(accountString)) {
            NToast.shortToast(mContext, R.string.phone_number_is_null);
            mAccountEdit.setShakeAnimation();
            return;
        }


        if (TextUtils.isEmpty(passwordString)) {
            NToast.shortToast(mContext, R.string.password_is_null);
            mPasswordEdit.setShakeAnimation();
            return;
        }
        if (passwordString.contains(" ")) {
            NToast.shortToast(mContext, R.string.password_cannot_contain_spaces);
            mPasswordEdit.setShakeAnimation();
            return;
        }
        logoSubmit();
    }

    @Override
    public void onClick(View view) {
        if (DoubleClickUtil.check()) return;

        if (!ToolsKit.isNetworkAvailable(this)) {
            NToast.shortToast(this, "请检查是否联网");
            return;
        }

        switch (view.getId()) {
            case R.id.login_btn:
                checkLogin();

                break;

        }
    }


    private void logoSubmit() {
        com.lzy.okgo.model.HttpHeaders headers = new com.lzy.okgo.model.HttpHeaders();
        HttpParams params = new HttpParams();
        params.put("name", accountString);
        params.put("psw", passwordString);
        params.put("openid", "0");


        OkGo.post(ServerAddress.BEAN_UTILCONTROLLER_LOGIN).tag(mContext).headers(headers).params(params)
                .dialogIsShow(true)
                .tostIsShow(false)
                .cacheMode(CacheMode.NO_CACHE).execute(new StringEluneCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                if (ToolsKit.isStringNotEmpty(s) && !"0".equals(s)) {
                    editorBase.putString(SealConst.SEALTALK_LOGING_ACCOUNT, accountString);
                    editorBase.putString(SealConst.SEALTALK_LOGING_PASSWORD, passwordString);
                    editorBase.putString(SealConst.SEALTALK_USERID, s);
                    editorBase.commit();
                    startActivity(new Intent(mContext, MainActivity.class));
                    finish();
                } else {
                    toastActBtm("登录失败，账号或密码错误");
                }


            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                toastActBtm("登录失败，请重试");
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}

